// pages/ticket/ticket-lists/ticket-lists.js
var httpUtil = require('../../../utils/httpUtil.js');
var uiUtil = require('../../../utils/uiUtil.js');
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketLists: {
      totalCount: null,
      pageCount: null,
      pageNo: "0",
      pageSize: "30",
      memberCardList:[]
    },

    requestGoodsTask: null, //请求task
    isLoadingMore: false,
    enableLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.getTicketLists();
  },

  /**
   * 获取会员卡列表
   */
  getTicketLists() {
    let pageNo = (parseInt(that.data.ticketLists.pageNo) + 1).toString();
    let pageSize = that.data.ticketLists.pageSize;
    let memberCardList = that.data.ticketLists.memberCardList;
    let totalCount = that.data.ticketLists.totalCount;

    var lastRequestTask = that.data.requestGoodsTask;
    if (lastRequestTask) {
      try {
        lastRequestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.ticketLists.pageNo
    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }

    var requestTask =httpUtil.PostRequest("mcard_list", {
      pageNo: pageNo,
      pageSize: pageSize
    }, {
      success(res) {
        if (0 == that.data.ticketLists.totalCount) {
          res.memberCardList = res.memberCardList || []
        } else {
          res.memberCardList = memberCardList.concat(res.memberCardList || []);
        }
        that.setData({
          ticketLists: res,
          enableLoadMore: res.memberCardList.length < res.totalCount
        })
      },
      fail(code, res) {
        that.setData({
          enableLoadMore: false,
          fail:true
        })
        uiUtil.showToast(res);
      },
      complete() { 
        that.setData({
          isLoadingMore: false
        })
      }
    })
    that.setData({
      requestGoodsTask: requestTask
    })
  },

  

  /**
   * 滑动到底部
   */
  onReachBottom: function () {
    this.getTicketLists();
  },

  
  /**
  * 跳转会员卡详情页面
  */
  goShopDetails(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    if (item) {
      wx.navigateTo({
        // url: '/pages/shop/shop/shop?id=' + item.id + "&page=page3&mchId=" + item.mchId
        url: '/pages/ticket/ticket-details/ticket-details?id=' + item.id + "&page=page3&mchId=" + item.mchId+"&index="+index,
      })
    }
  },
})